<?php namespace api\common\service;
/**
 * Created by PhpStorm.
 * User: 
 * Date: 2019/12/6
 * Time: 10:27
 */
use api\common\model\RlHiseAreaModel;
use api\common\model\RlHiseQxTableModel;
use api\common\model\RlHiseDqTableModel;
use think\Db;
/**
 * 全局函数
 * Class CurlService
 * @package common\service
 */
class FunctionService
{
    /**
     * 生成短信验证码
     * @param []
     * @return []
     * @date 2019/11/5 11:39 AM
     */
    public static function random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }

    /**
     * 获取所有地区信息
     */
    public static function areaall()
    {
        $rlHiseAreaModel = new RlHiseAreaModel();
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        $rlHiseQxTableModel = new RlHiseQxTableModel();
        $rlHiseArea = $rlHiseAreaModel->where('active_flag',1)->select()->toArray();
        $rlHiseDqTable = $rlHiseDqTableModel->where('active_flag',1)->select()->toArray();
        $rlHiseQxTable = $rlHiseQxTableModel->where('active_flag',1)->select()->toArray();
        foreach ($rlHiseDqTable as $ke => $valu) {
                foreach ($rlHiseQxTable as $k => $val) {
                        if($val['dq_code'] == $valu['dq_code'])
                        {
                            $rlHiseDqTable[$ke]['children'][] = $val;
                        }
                }
            }
        foreach ($rlHiseArea as $key => $value) {
            foreach ($rlHiseDqTable as $ke => $valu) {
                if($valu['sf_code'] == $value['sf_code'])
                {
                    $rlHiseArea[$key]['children'][] = $valu;
                }
            }

        }
        return $rlHiseArea;
    }
    /**
     * 获取省信息
     */
        public static function area()
    {
        $rlHiseAreaModel = new RlHiseAreaModel();
        $result = $rlHiseAreaModel->where('active_flag',1)->select()->toArray();
        return $result;
    }

    /**
     * 获取地区信息
     *@param string $sf_code   省编号
     */
    public static function dqtable($sf_code=0)
    {
        $rlHiseDqTableModel = new RlHiseDqTableModel();
        if($sf_code)
        {
            $result = $rlHiseDqTableModel->where('sf_code',$sf_code)->where('active_flag',1)->select()->toArray();
        }else{
            $result = $rlHiseDqTableModel->where('active_flag',1)->select()->toArray();
        }
        return $result;
    }

    /**
     * 获取区县信息
     *@param string $dq_code   省编号
     */
    public static function qxtable($dq_code=0)
    {
        $rlHiseQxTableModel = new RlHiseQxTableModel();
        if($dq_code)
        {
            $result = $rlHiseQxTableModel->where('dq_code',$dq_code)->where('active_flag',1)->select()->toArray();
        }else{
            $result = $rlHiseQxTableModel->where('active_flag',1)->select()->toArray();
        }
        return $result;
    }

    /**
     * 将数组中，大写的键名转换为小写
     * @param []
     * @return []
     * @date 2019/12/16 1:04 PM
     */
    public static function arraykeyToLower( $data = array() )
    {
        if( !empty($data) )
        {
            //一维数组
            if(count($data) == count($data,1))
            {
                $data = array_change_key_case($data , CASE_LOWER);
            }
            else    //二维数组
            {
                foreach( $data as $key => $value )
                {
                    $data[$key] = array_change_key_case($value , CASE_LOWER);
                }
            }
        }
        return $data;
    }

    /**
     * 将数组中，小写的键名转换为大写
     * @param []
     * @return []
     * @date 2019/12/16 1:04 PM
     */
    public static function arraykeyToUPPER( $data = array() )
    {
        if( !empty($data) )
        {
            //一维数组
            if(count($data) == count($data,1))
            {
                $data = array_change_key_case($data , CASE_UPPER);
            }
            else    //二维数组
            {
                foreach( $data as $key => $value )
                {
                    $data[$key] = array_change_key_case($value , CASE_UPPER);
                }
            }
        }
        return $data;
    }

    //图片上传方法
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        if(!$file)
        {
            return ['code'=>0,'msg' => '文件不能为空'];
        }
        // 移动到框架应用根目录/uploads/ 目录下
        // $info = $file->validate(['size'=>4194304])->move( '../uploads');
        $info = $file->move( '../uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            // echo $info->getExtension();
            $type = $info->getExtension();
            if(!in_array($type, ['jpg','png','jpeg']))
            {
                return ['code'=>0,'msg' => '文件格式不正确'];
            }
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename(); 
            return ['code'=>1,'img'=>'/uploads/'.$info->getSaveName(),'msg'=>''];
        }else{
            // 上传失败获取错误信息
            return ['code'=>0,'msg' => $file->getError()];
        }
    }


    //安装单查询条件整理方法
    public function selectazd($data)
    {
        $where=[];
        //用户名字
        if(isset($data['yh_name']) && $data['yh_name'])
        {
            $where['yh_name'] = ['like','%'.$data['yh_name'].'%'];
        }
        //安装单id
        if(isset($data['azd_id']) && $data['azd_id'])
        {
            $where['azd_id'] = ['like','%'.$data['azd_id'].'%'];
        }
        //用户手机号
        if(isset($data['sj_one']) && $data['sj_one'])
        {
            $where['sj_one'] = ['like','%'.$data['sj_one'].'%'];
        }
        //详细地址
        if(isset($data['address']) && $data['address'])
        {
            $where['address'] = ['like','%'.$data['address'].'%'];
        }
        //工程状态
        if(isset($data['gczt']))
        {
            $where['gczt'] = $data['gczt'];
        }
        //是否申请延期
        if(isset($data['is_apply_delay']))
        {
            $where['is_apply_delay'] = $data['is_apply_delay'];
        }
        //延期是否通过
        if(isset($data['apply_delay_status']))
        {
            if($data['apply_delay_status']==1)
            {
                $where['yqsqzq'] = ['>',0];
                $where['is_apply_delay'] = 1;
            }
            if($data['apply_delay_status']==2)
            {
                $where['yqsqzq'] = 0;
                $where['is_apply_delay'] = 1;
            }
            if($data['apply_delay_status']==0)
            {
                $where['yqsqzq'] = 0;
                $where['is_apply_delay'] = 1;
            }
        }
        //是否巡检
        if(isset($data['sfxjbs']) && in_array($data['sfxjbs'],[0,1]))
        {
            $where['sfxjbs'] = $data['sfxjbs'];
        }
        //巡检是否有问题
        if(isset($data['is_problem']))
        {
            if($data['is_problem']==1)
            {
                $azd_id = Db::name('rl_inspect')->where('is_problem',1)->where('inspect_status',2)->column('azd_id');
                $where['azd_id'] = ['in',$azd_id];
            }
            if($data['is_problem']==0)
            {
                $azd_id = Db::name('rl_inspect')->where('is_problem',0)->where('inspect_status',2)->column('azd_id');
                $where['azd_id'] = ['in',$azd_id];
            }
        }
        //经销商名字
        if(isset($data['jxs_mc']) && $data['jxs_mc'])
        {
            $jxsbm = Db::name('rl_et_customer')->where('jxs_mc','like','%'.$data['jxs_mc'].'%')->column('jxsbm');
            $where['jxs'] = ['in',$jxsbm];
        }
        //安装商名字
        if(isset($data['azs_mc']) && $data['azs_mc'])
        {
            $server_code = Db::name('rl_et_azs')->where('server_name','like','%'.$data['azs_mc'].'%')->column('server_code');
            $where['azs'] = ['in',$server_code];
        }
        //安装队名字和手机号
        if(isset($data['engineer_name'])||isset($data['azdphone']))
        {
            $azdwhere=[];
            if(isset($data['azdphone']) && $data['azdphone'])
            {
                $azdwhere['phone'] = ['like','%'.$data['azdphone'].'%'];
            }
            if(isset($data['engineer_name']) && $data['engineer_name'])
            {
                $azdwhere['engineer_name'] = ['like','%'.$data['engineer_name'].'%'];
            }
            $engineer_code = Db::name('rl_et_azd')->where($azdwhere)->column('engineer_code');
            $where['azd'] = ['in',$engineer_code];
        }
        //销售名字
        if(isset($data['jxsxsy']) && $data['jxsxsy'])
        {
            $where['jxsxsy'] = ['like','%'.$data['jxsxsy'].'%'];
        }
        //金卡号
        if(isset($data['jkh']) && $data['jkh'])
        {
            $where['jkh'] = ['like','%'.$data['jkh'].'%'];
        }
        //销售手机
        if(isset($data['xsy_sj']) && $data['xsy_sj'])
        {
            $where['xsy_sj'] = ['like','%'.$data['xsy_sj'].'%'];
        }
        //条码编号
        if(isset($data['jqxlh']) && $data['jqxlh'])
        {
            $azd_id = Db::table('cmf_rl_hise_service_pro_inst_detail')->where('jqxlh',$data['jqxlh'])->column('azd_id');
            $where['azd_id'] = ['in',$azd_id];
        }
        //分公司名字
        if(isset($data['fgs_mc']) && $data['fgs_mc'])
        {
            $fwfgs = Db::name('rl_company')->where('orgname','like',$data['fgs_mc'])->column('orgdept');
            $where['fwfgs'] = ['in',$fwfgs];
        }
        //签约分公司名字
        if(isset($data['qyfgs_mc']) && $data['qyfgs_mc'])
        {
            $fwfgs = Db::name('rl_company')->where('orgname','like',$data['fgs_mc'])->column('orgdept');
            $where['qyfgs'] = ['in',$fwfgs];
        }
        //跨区审核状态
        if(isset($data['kqshzt']) && $data['kqshzt'])
        {
            $where['kqshzt'] = $data['kqshzt'];
        }
        return $where;
    }
}